package water_ruler_threads;

import com.zhejiangzhengyuan.municipal_common.service.*;
import com.zhejiangzhengyuan.municipal_common.utils.LED.URLUtils;
import com.zhejiangzhengyuan.municipal_common.utils.waterRuler.ByteAndStringUtils;
import com.zhejiangzhengyuan.municipal_common.utils.waterRuler.DataUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class ServerThread implements Runnable {

  private static ExecutorService executorService1 = Executors.newFixedThreadPool(10);

  private static ExecutorService executorService2 = Executors.newFixedThreadPool(10);

  private Socket socket;
  private DeviceService deviceService;
  private DeviceMonitorValueService deviceMonitorValueService;
  private DeviceDataService deviceDataService;
  private WarnEventService warnEventService;
  private ElectronicDataService electronicDataService;
  private LedBindingService ledBindingService;
  private LedHistoryService ledHistoryService;
  private URLUtils urlUtils;

  public ServerThread(
      Socket socket,
      DeviceService deviceService,
      DeviceMonitorValueService deviceMonitorValueService,
      DeviceDataService deviceDataService,
      WarnEventService warnEventService,
      ElectronicDataService electronicDataService,
      LedBindingService ledBindingService,
      LedHistoryService ledHistoryService,
      URLUtils urlUtils) {
    this.socket = socket;
    this.deviceService = deviceService;
    this.deviceMonitorValueService = deviceMonitorValueService;
    this.deviceDataService = deviceDataService;
    this.warnEventService = warnEventService;
    this.electronicDataService = electronicDataService;
    this.ledBindingService = ledBindingService;
    this.ledHistoryService = ledHistoryService;
    this.urlUtils = urlUtils;
  }

  public void run() {
    DataInputStream dataInputStream = null;
    try {
      dataInputStream = new DataInputStream(socket.getInputStream());
      // 实际上上每次传送44个字节，这里我们用45接收
      byte[] bytes = new byte[45];
      // 翻译用户发送的信息，并打印在控制台
      while (dataInputStream.read(bytes) != -1) {
        String allData = ByteAndStringUtils.byteToHex(bytes);
        log.info("所有数据:{}", allData);
        // 这是第三次握手，设备第二次发送数据给服务端
        if (DataUtils.Get88BitData(allData) != null) {
          executorService2.execute(
              new ReceiveThread(
                  socket,
                  allData,
                  deviceService,
                  deviceMonitorValueService,
                  deviceDataService,
                  warnEventService,
                  electronicDataService,
                  ledBindingService,
                  ledHistoryService,
                  urlUtils));
        } else {
          String s1 = DataUtils.Get44itData(allData);
          String bcc = DataUtils.sendData(s1);
          // 第一次连接
          executorService1.execute(new SendDataThread(socket, ByteAndStringUtils.hexToByte(bcc)));
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
